package com.czwj.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.ProjectItemMapper;
import com.czwj.dao.SafetyInspectionMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.SafetyInspection;
import com.czwj.service.SafetyInspectionService;

@Service
public class SafetyInspectionServiceImpl implements SafetyInspectionService {

	@Resource
	SafetyInspectionMapper mapper;
	@Resource
	AttachmentMapper amapper;
	@Resource
	AttachmentRelyMapper relymapper;
	@Resource
	ProjectItemMapper itemmapper;
	
	@Value("${resources.phypath}")
	private String PhyPath;

	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_SafetyInspection_Path = "安全巡检/";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@Override
	public List<String> existinspection(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.existinspection(map);
	}

	@Override
	public List<SafetyInspection> findonday(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.findoneday(map);
	}

	@Transactional
	@Override
	public int insert(List<SafetyInspection> list, MultipartFile[] images, Date inspectiondate, Integer userid,
			String ip,Integer status) {
		// TODO Auto-generated method stub
		int count = 0;
		SafetyInspection safetyInspection = new SafetyInspection();
		for (SafetyInspection safe : list) {
			safe.setInspectiondate(inspectiondate);
			safe.setCreatedate(new Date());
			safe.setCreaterid(userid);
			safe.setCreateip(ip);
			safe.setStatus(status);
			count += mapper.insertSelective(safe);
			if(list.indexOf(safe)==list.size()-1){
				safetyInspection = safe;
			}
		}
		String date = sdf.format(inspectiondate);
		ProjectItem item = this.itemmapper.selectByPrimaryKey(safetyInspection.getProjectitemid());
		String path = item.getName()+"/"+Attachment_SafetyInspection_Path+date+"/";
		String sourcepath = PhyPath+path;//存储路径
		String urlpath = ResPath+path;//资源路径
		File dest = new File(sourcepath);
		if (!dest.exists()) {
			dest.mkdirs();
		}
		for (MultipartFile image : images) {
			String filename = image.getOriginalFilename();
			if(!filename.contains(".")) {
				filename+=".jpg";
			}
			String _sourcepath = sourcepath+filename;
			String _urlpath =urlpath+filename;
			File file = new File(_sourcepath);
			while (file.exists()) {
				String[] arr =  _sourcepath.split("\\.");
				_sourcepath="";
				for(int i=0;i<arr.length;i++){
					if(i<arr.length-1){
						_sourcepath += arr[i];;
					}else{
						_sourcepath+="(1)."+arr[i];
					}
				}
				file = new File(_sourcepath);
			}
			try {
				image.transferTo(new File(_sourcepath));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return 0;
			}
			Attachment attachment = new Attachment(filename, _urlpath, _sourcepath, "image", userid, new Date(), ip);
			count+=this.amapper.insertSelective(attachment);
			AttachmentRely aRely = new AttachmentRely("safetyinspection", safetyInspection.getId(), attachment.getId());
			count+=this.relymapper.insertSelective(aRely);
		}
		return count;
	}

	/** 
	* 
	* @return
	* @see com.czwj.service.SafetyInspectionService#selectTosafe()
	*/
	@Override
	public List<SafetyInspection> selectTosafe(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.selectTosafe(map);
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.SafetyInspectionService#selectTosafeInfo(java.util.Map)
	*/
	@Override
	public List<SafetyInspection> selectTosafeInfo(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.selectTosafeInfo(map);
	}

	/** 
	* 
	* @param date
	* @return
	* @see com.czwj.service.SafetyInspectionService#updateStatus(java.util.Date)
	*/
	@Override
	public int updateStatus(Date date){
		// TODO Auto-generated method stub
		return this.mapper.updateStatus(date);
	}

	
}
